package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1 设置编码方式
        // 2 读取action,并判断
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        // 3 根据action进行执行不同的代码
        if("selectAll".equals(action)){
            // 3.1 接受数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String communityName = req.getParameter("communityName");
            // 3.2 封装到map集合
            Map<String,String> map = new HashMap();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);
            // 3.3 连接数据库操作
            // 3.3.1 查询所有的数据
            List<Community> list = communityDao.selectAll(map);
            // 3.3.2 查询总条数
            int total = communityDao.selectCount(map);
            // 3.4 给出响应信息
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
